// basicnpc.txt
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.

begincreaturescript;

variables;

short i,target;
short party_sighted;

body;

set_char_script_mode(ME,3);

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	break;

beginstate DEAD_STATE;

if (get_sdf(110,1) == 1) {
 set_flag(110,1,10);
 message_dialog("Valzier falls in battle! Fortunately, he is not dead, just severely wounded. He will no longer be able to fight with you. If you enter a friendly town, he will be restored.","");
 erase_char(ME);
}

if (get_sdf(110,1) == 2) {
 set_flag(110,1,3);
 message_dialog("You manage to slay the treacherous Valzier, if that even was his real name. The obsessive maniac betrayed his tribe of Asmur and led Arivan's forces here all in the name of some insane desire for his leader.","Now he is dead and the resistance has fallen. You have no choice but to continue to the teleporter now. There is no turning back.");
 award_party_xp(300,5);
}

break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (select_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		do_attack();
		set_state(3);
		}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.

	if (can_see_char(1000))
		party_sighted = 1;
	if ((get_flag(1,4) == 0) && (dist_to_party() <= 8) && (party_sighted > 0)) {
		if (dist_to_party() <= 2) {
			set_flag(1,4,1);
   set_mobility(ME,1);
			begin_talk_mode(1);
			}
			else approach_char(ME,random_group_member(0),2);
		}


if ((get_flag(1,4) == 1) && (dist_to_waypoint(ME,2) > 0)) {
	if ((get_terrain(28,12) == 12) && (dist_to_waypoint(ME,0) > 0))
  approach_waypoint(ME,0,0);
 if ((get_terrain(28,12) == 12) && (dist_to_waypoint(ME,0)) <= 0) {
  set_terrain(28,12,16);
  if (can_see_char(1000))
   play_sound(58);
 }
	if ((get_terrain(29,10) == 12) && (dist_to_waypoint(ME,1) > 0))
  approach_waypoint(ME,1,0);
 if ((get_terrain(29,10) == 12) && (dist_to_waypoint(ME,1)) <= 0) {
  set_terrain(29,10,16);
  if (can_see_char(1000))
   play_sound(58);
 }
 approach_waypoint(ME,2,0);
}
if ((get_flag(1,4) == 1) && (dist_to_waypoint(ME,2)) <= 0) {
 set_mobility(ME,0);
 set_character_facing(ME,6);
}

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_sdf(1,4) == 0) {
  set_mobility(ME,1);
  set_flag(1,4,1);
  begin_talk_mode(1);
 }
 if (get_sdf(1,4) == 1)
  print_str("Talking: Valzier does not respond.");
 if (get_sdf(1,4) > 1)
  begin_talk_mode(get_memory_cell(3));
break;